/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn;

import cn.webrx.mapper.BookMapper;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.sql.Connection;

/**
 * <p>Project: mybatis2021 - Demo
 * <p>Powered by webrx On 2021-10-27 11:05:35
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@Slf4j
public class Demo {

    BookMapper bm = null;
    Connection conn = null;
    SqlSession session = null;
    SqlSessionFactory ssf = null;

    @Before
    public void init() {
        //建立数据源 HikariDataSource
        var ds = new HikariDataSource();
        ds.setJdbcUrl("jdbc:mysql:/wxdb");
        ds.setUsername("root");

        //建立事务对象
        var tx = new JdbcTransactionFactory();
        //建立环境对象
        var env = new Environment("development", tx, ds);
        //建立配置对象相当于mybatis-config.xml文件
        var cfg = new Configuration(env);

        //指定别名 java.lang.String 默认的别名说法string
        //指定别名cn.webrx.model.Book  可以直接写book不分区大小写
        var alias = cfg.getTypeAliasRegistry();
        alias.registerAliases("cn.webrx.model");
        alias.registerAliases("cn.webrx.vo");
        alias.registerAliases("cn.webrx.pojo");
        alias.registerAliases("cn.webrx.entity");

        cfg.addMappers("cn.webrx.mapper"); //会自动加载接口类，同时也加载接口同名的同位置的xml

        //加载resouces/mappers/*.xml
        //var xml = "mappers/xyz.xml";
        //var is = Thread.currentThread().getContextClassLoader().getResourceAsStream(xml);
        //System.out.println(is);
        //new XMLMapperBuilder(is, cfg, xml, cfg.getSqlFragments()).parse();


        //会自动批量加载/resources/mappers/*.xml
        var xml = "mappers/";
        var p = Thread.currentThread().getContextClassLoader().getResource(xml).getPath();
        var f = new File(p);
        for (String s : f.list()) {
            if (s.endsWith(".xml")) {
                var t = xml + s;
                var is = Thread.currentThread().getContextClassLoader().getResourceAsStream(t);
                new XMLMapperBuilder(is, cfg, t, cfg.getSqlFragments()).parse();
            }
        }


        var sf = new SqlSessionFactoryBuilder().build(cfg);
        ssf = sf;
        var ss = sf.openSession(true);
        session = ss;

        //jdbc 接口
        conn = ss.getConnection();
        bm = ss.getMapper(BookMapper.class);
    }

    @After
    public void close() {

    }

    @Test
    public void t2(){
        var s1 = ssf.openSession();
        var s2 = ssf.openSession();
        var s3 = ssf.openSession();
        //System.out.println(s1 == s2);

        var bm1 = s1.getMapper(BookMapper.class);
        //System.out.println(bm1.queryAll());
        System.out.println(bm1.show());
        s1.commit();

        System.out.println(bm1.show());
        System.out.println(bm1.show());
        //System.out.println(bm1.queryAll());
        //System.out.println(bm1.queryAll());


        var bm3 = s3.getMapper(BookMapper.class);
        //System.out.println(bm3.queryAll());
        System.out.println(bm3.show());
    }


    @Test
    public void t1() {
        bm.queryAll();
        session.clearCache();
        bm.queryAll();
        session.clearCache();
        bm.queryAll();
   }
}
